abidibo.net

Protected members in mootools classes

javascript mootools programming

Some times ago I wrote about the private static and instance members in mootools classes. Now it's time to speak about the protected members in mootools classes.

So the goal here is to obtain some members which are inherited from the parent class and accessible to a child class but not outside.

The trick is to use the Object.merge function to merge together the private properties of the parent exposed to the children through a protected method and the private properties of the child. Look at the following code:

  1. var ids = 1;

  2. var myParentClass = (function(){

  3.   var _protected_prop = {};

  4.   return new Class({

  5.     initialize: function(id, par1, par2) {
  6.       this.id = id;
  7.       _protected_prop[this.id] = {};
  8.       _protected_prop[this.id].prop1 = 'myvalue1';
  9.       _protected_prop[this.id].prop2 = true;
  10.       _protected_prop[this.id].prop3 = 65;
  11.     },
  12.     protectedProp: function() {
  13.       return _protected_prop[this.id];
  14.     }.protect()
  15.   });
  16. }());

  17. var myChildClass = (function() {

  18.   var _protected_prop = {};

  19.   return new Class({
  20.     Extends: myParentClass,
  21.     initialize: function(par1, par2, par3) {
  22.       var id = ids++;
  23.       this.parent(id, par1, par2);
  24.       // here the protected prop of the parent are ready
  25.       _protected_prop[this.id] = {
  26.         prop4: 'myvalue4',
  27.         prop2: false
  28.       };
  29.       _protected_prop[this.id] = Object.merge(this.protectedProp(),     _protected_prop[this.id]);
  30.     }
  31.   });
  32. }());

So what does it happen here?

The _proptected_prop of the parent class is a private member but exposed to the children thanks to the protect mootools method (line 18). The a child has it's own private object _protected_prop and creates the protected member by merging together its one and the parent one.

The Object.merge function alters and returns the parent protected_prop object so that really the child has a refeernce to the altereted parent object. Such thing is imporant as it assures that a change to a protected property through a parent method results in a change of the child proetcted property!

Look at the following jsfiddle snippet, it shows what explained above. (follow this link if you can't see it.

Subscribe to abidibo.net!

If you want to stay up to date with new contents published on this blog, then just enter your email address, and you will receive blog updates! You can set you preferences and decide to receive emails only when articles are posted regarding a precise topic.

I promise, you'll never receive spam or advertising of any kind from this subscription, just content updates.

Subscribe to this blog

Comments are welcome!

blog comments powered by Disqus

Your Smartwatch Loves Tasker!

Your Smartwatch Loves Tasker!

Now available for purchase!

Featured